<template>
  <div class="mycard">
    <header>
      <img
        :src="user.img?user.img:''"
        class="avater icon iconfont icon-lianxirenxuanzhong"
        @click="showBrief($event)"
      />
      <div
        class="brief-introduction"
        ref="briefIntroduction"
        v-show="brief"
        tabindex="0"
        @blur="closeBrief"
      >
        <div class="content">
          <div class="content-left">
            <div class="nickname">{{user.nickname}}</div>
            <div class="name">chatID:</div>
            <div class="chatid">{{user.wxid}}</div>
          </div>
          <div class="content-right">
            <img
              :src="user.img?user.img:''"
              alt
              class="avater icon iconfont icon-lianxirenxuanzhong"
            />
          </div>
        </div>
        <div class="btn">
          <span class="icon iconfont icon-xiangqing" title="详情" @click="selectFriend(0)"></span>
        </div>
      </div>
    </header>
    <div class="navbar">
      <router-link to="/chat" class="icon iconfont icon-map_icon_msg"></router-link>
      <span class="chat-dot" v-show="chatNum > 0">{{chatNum}}</span>
      <router-link to="/friend" class="icon iconfont icon-icon_friend"></router-link>
      <span class="friend-dot" v-show="friendRequestNum > 0">{{friendRequestNum}}</span>
    </div>
    <footer>
      <i class="icon iconfont icon-menu-fill" @click="state(1)" @blur="state(2)" tabindex="0"></i>
      <div class="menu" v-show="flag" @mouseover="mouseover('over')" @mouseout="mouseover('out')">
        <div class="menu-item" @click="inClearChatList()">清空聊天记录</div>
        <div class="menu-item" @click="logout()">退出</div>
      </div>
    </footer>
  </div>
</template>

<script>
import { mapState, mapActions } from "vuex";
export default {
  data() {
    return {
      flag: false,
      mouse: false,
      brief: false
    };
  },
  computed: {
    ...mapState(["user", "friendRequestNum", "chatNum"])
  },
  methods: {
    ...mapActions(["logout", "clearChatList", "selectFriend"]),
    inClearChatList() {
      this.flag = false;
      this.clearChatList();
    },
    state(value) {
      if (value == 2 && !this.mouse) {
        this.flag = false;
        return;
      }
      if (value == 1) {
        if (this.flag) {
          this.flag = false;
        } else {
          this.flag = true;
        }
      }
    },
    mouseover(value) {
      if (value == "over") this.mouse = true;
      if (value == "out") this.mouse = false;
    },
    showBrief(event) {
      let box = this.$refs.briefIntroduction;
      // 解决兼容性问题
      event = event || window.event;

      let left = event.offsetX;
      let top = event.offsetY;

      box.style.left = left + "px";
      box.style.top = top + "px";

      // 给窗口聚焦
      setTimeout(() => box.focus(), 3);

      this.brief = true;
    },
    closeBrief() {
      this.brief = false;
    }
  }
};
</script>

<style scoped>
.mycard {
  position: relative;
  width: 100%;
  height: 100%;
}

header {
  position: relative;
  width: 36px;
  height: 36px;
  margin: 20px 12px 0 12px;
  background-color: #fff;
  border-radius: 2px;
  z-index: 1;
}

.avater {
  widows: 100%;
  height: 100%;
  border-radius: 2px;
}

.icon-lianxirenxuanzhong {
  font-size: 36px;
  color: darkgray;
}

.brief-introduction {
  position: absolute;
  top: 0;
  left: 0;
  width: 260px;
  height: 140px;
  background-color: #fff;
  padding: 30px;
  outline: none;
}

.content {
  display: flex;
  height: 85px;
  border-bottom: 1px solid #f4f4f4;
}

.content-left {
  width: 195px;
}

.name {
  float: left;
  width: 60px;
  line-height: 20px;
  color: #999999;
}

.chatid {
  display: inline-block;
  width: 130px;
  line-height: 20px;
  word-break: break-word;
  color: #999999;
}

.nickname {
  margin-bottom: 10px;
  font-size: 20px;
}

.content-right img {
  width: 65px;
  height: 65px;
}

.btn {
  height: 54px;
  line-height: 54px;
  text-align: end;
}

.icon-xiangqing {
  font-size: 24px;
  vertical-align: text-top;
  color: #818181;
}

.icon-xiangqing:hover {
  color: #303030;
}

.navbar {
  position: relative;
  width: 100%;
  text-align: center;
}

.navbar .icon {
  display: inline-block;
  font-size: 26px;
  margin-top: 28px;
  padding: 0 16px;
  box-sizing: border-box;
  color: rgb(173, 174, 175);
  opacity: 0.8;
  cursor: pointer;
}

.navbar .icon:hover {
  opacity: 1;
}

.navbar .active {
  color: rgb(0, 220, 65);
}

.navbar .icon-map_icon_msg {
  font-size: 22px;
  padding: 0px 19px;
}

footer {
  position: absolute;
  width: 100%;
  bottom: 25px;
  text-align: center;
}

footer i {
  font-size: 30px;
  color: rgb(173, 174, 175);
  cursor: pointer;
  outline: none;
}

.menu {
  width: 110px;
  text-align: left;
  /* padding: 5px 0; */
  position: absolute;
  left: 60px;
  bottom: 10px;
  background-color: #2d2d2e;
  cursor: pointer;
  z-index: 10;
}

.menu-item {
  height: 40px;
  line-height: 40px;
  font-size: 15px;
  padding-left: 5px;
  color: #8c8c8c;
}

.menu-item:hover {
  background-color: #38383a;
}

.friend-dot {
  position: absolute;
  display: inline-block;
  bottom: 15px;
  right: 10px;
  font-size: 12px;
  border-radius: 8px;
  padding: 1px 5px 3px;
  color: #fff;
  background-color: red;
}

.chat-dot {
  position: absolute;
  display: inline-block;
  top: 20px;
  right: 10px;
  font-size: 12px;
  border-radius: 8px;
  padding: 1px 5px 3px;
  color: #fff;
  background-color: red;
}
</style>